//
//  MyOrderCarViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/9/28.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "MyOrderCarViewController.h"
#import "MyOrderCarTableViewCell.h"
#import "ASubmitOrderFormViewController.h" //提交预订单

@interface MyOrderCarViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mytableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *mydateSource;
@property (nonatomic, strong) UIView *tabFootView;
@property (nonatomic, strong) NSMutableSet <NSString *>*saoYiSaoStrSet;
@property (nonatomic, strong) UIView *bgViewsec;
@property (nonatomic, strong) UIView *bgViewsecDele;
@property (nonatomic, strong) UIButton *secSelBtn; //分区区头选择按钮
@property (nonatomic, assign) BOOL rightChose;
@property (nonatomic, assign) BOOL secChose;
@property (nonatomic, assign) BOOL rowChose;
@property (nonatomic, assign) BOOL allChose;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIButton *allSelBtn;
@property (nonatomic, strong) UIButton *orderNowBtn;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation MyOrderCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的预订";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    
    [self requestData];
    [self setupRightItem];
    [self showdetaile];
    [self shwoBgviewsecDele];
    //[self showtabFootView];
    // Do any additional setup after loading the view.
}

#pragma mark 设置右上角按钮
- (void)setupRightItem {
    _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(KWIDTH - 70,100, 50, 50)];
    [_rightButton setTitle:@"管理" forState:UIControlStateNormal];
    [_rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _rightButton.titleLabel.font = FontSize(14);
    [_rightButton addTarget:self action:@selector(manageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:_rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark 右上角按钮的点击方法
- (void)manageBtnAction:(UIButton *)button {
    //_rightButton.enabled = NO;
    self.rightChose =! self.rightChose;
    if (self.rightChose) {
        [_rightButton setTitle:@"完成" forState:UIControlStateNormal];
        self.orderNowBtn.hidden = YES;
    } else {
        [_rightButton setTitle:@"管理" forState:UIControlStateNormal];
        self.orderNowBtn.hidden = NO;
    }
    NSLog(@"您点击了 导航条 右侧 的管理按钮");
}

//我的预订 数据请求
- (void)requestData {
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"user_type"] = @"1"; //用户类型
    
//    [weakSelf.mydateSource removeAllObjects];
//    [NetWorkTool POST:myOrderCar param:param success:^(id dic) {

//    self.mydateSource = [CSmaintenanceListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
    
//        //[self.mytableView reloadData];
//        //[self.mytableView.mj_header endRefreshing];
//
//    } other:^(id dic) {
//        //[weakSelf.mydateSource removeAllObjects];
//        //[self.mytableView reloadData];
//        //[self.mytableView.mj_header endRefreshing];
//
//    } fail:^(NSError *error) {
//        //[weakSelf.mydateSource removeAllObjects];
//        //[self.mytableView reloadData];
//        //[self.mytableView.mj_header endRefreshing];
//
//    } needUser:YES];
}

-(void)showdetaile {
    
    [self.view addSubview:self.scrollView];
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 56)];
    
    _mytableView = [[UITableView alloc] init];
    _mytableView.frame = CGRectMake(0, kNaviHeight +10, KWIDTH, KHEIGHT-kNaviHeight-56-10);
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.estimatedRowHeight = 90;
    _mytableView.rowHeight = UITableViewAutomaticDimension;
    _mytableView.tableFooterView = [UIView new];
    [_mytableView registerClass:[MyOrderCarTableViewCell class] forCellReuseIdentifier:@"MyOrderCarTableViewCell"];
    _mytableView.backgroundColor = [UIColor clearColor];
    adjustInset(_mytableView);
    [_mytableView reloadData];
    _mytableView.tableFooterView = self.tabFootView;
    [_scrollView addSubview:_mytableView];
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.frame = CGRectMake(0, _mytableView.bottom, KWIDTH, 56);
    bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:bottomView];
    
    //全选按钮
    UIButton *allSelBtn = [[UIButton alloc] init];
    allSelBtn.backgroundColor = [UIColor whiteColor];
    allSelBtn.frame = CGRectMake(10, 8, 90, 40);
    [allSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
    [allSelBtn setTitle:@"全选" forState:UIControlStateNormal];
    allSelBtn.titleLabel.font = FontSize(14);
    [allSelBtn setTitleColor:[UIColor colorWithHexString:@"#484848"] forState:UIControlStateNormal];
    [allSelBtn addTarget:self action:@selector(bottomSelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:allSelBtn];
    self.allSelBtn = allSelBtn;
    
    //删除按钮
    UIButton *deleteBtn = [[UIButton alloc] init];
    deleteBtn.backgroundColor = [UIColor whiteColor];
    deleteBtn.frame = CGRectMake(bottomView.right-187, 8, 169, 42);
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = FontSize(14);
    deleteBtn.layer.borderColor = [UIColor redColor].CGColor;
    deleteBtn.layer.borderWidth = 1;
    deleteBtn.layer.cornerRadius = 21;
    deleteBtn.layer.masksToBounds = YES;
    [deleteBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:deleteBtn];
    self.deleteBtn = deleteBtn;
    
    //我要预订按钮
    UIButton *orderNowBtn = [[UIButton alloc] init];
    orderNowBtn.backgroundColor = [UIColor colorWithHexString:@"#70BE68"];
    orderNowBtn.frame = CGRectMake(bottomView.right-187, 8, 169, 42);
    [orderNowBtn setTitle:@"我要预订" forState:UIControlStateNormal];
    orderNowBtn.titleLabel.font = FontSize(14);
    orderNowBtn.layer.cornerRadius = 21;
    orderNowBtn.layer.masksToBounds = YES;
    [orderNowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [orderNowBtn addTarget:self action:@selector(orderNowBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:orderNowBtn];
    self.orderNowBtn = orderNowBtn;
}

//底部 全选按钮
- (void)bottomSelBtnAction:(UIButton *)button {
    self.allChose =! self.allChose;
    if (self.allChose) {
        [self.allSelBtn setImage:[UIImage imageNamed:@"seleQuyuyew"] forState:UIControlStateNormal];
    } else {
        [self.allSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
    }
    NSLog(@"您点击了 底部的 全选 按钮");
}

//我要预订按钮 点击事件处理
- (void)orderNowBtnAction:(UIButton *)button {
    ASubmitOrderFormViewController *submitVC = [[ASubmitOrderFormViewController alloc] init];
    [self.navigationController pushViewController:submitVC animated:YES];
    NSLog(@"您点击了 底部的 我要预订 按钮");
}

//删除按钮 点击事件处理
- (void)deleteBtnAction:(UIButton *)button {
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgViewsecDele];
    NSLog(@"您点击了 底部的 删除 按钮");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //return _mydateSource.count;
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 97;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.frame = CGRectMake(20, 0, KWIDTH-40, 40);
    
    UIButton *secSelBtn = [[UIButton alloc] init];
    //secSelBtn.backgroundColor = [UIColor redColor];
    secSelBtn.frame = CGRectMake(3, 3, 30, 30);
    [secSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
    [secSelBtn addTarget:self action:@selector(secSelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:secSelBtn];
    self.secSelBtn = secSelBtn;
    
    UILabel *shopTitLab = [[UILabel alloc] init];
    //shopTitLab.backgroundColor = [UIColor yellowColor];
    shopTitLab.frame = CGRectMake(secSelBtn.right +2, 9, KWIDTH-20-6-30-20-10, 18);
    shopTitLab.text = @"格力配件店铺";
    shopTitLab.numberOfLines = 0;
    shopTitLab.font = FontSize(14);
    shopTitLab.textColor = K333333;
    [headerView addSubview:shopTitLab];
    //self.shopTitLab = shopTitLab;
    
    UIImageView *topBgImg = [[UIImageView alloc] init];
    //topBgImg.backgroundColor = [UIColor orangeColor];
    topBgImg.frame = CGRectMake(shopTitLab.right +3, 8, 20, 15);
    [topBgImg setImage:imgname(@"jinruimage")];
    [headerView addSubview:topBgImg];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    line.frame = CGRectMake(0, 39, KWIDTH, 1);
    [headerView addSubview:line];
    
    return headerView;
}

//分区区头 选择按钮 点击事件处理
- (void)secSelBtnAction:(UIButton *)button {
    self.secChose =! self.secChose;
    if (self.secChose) {
        [_secSelBtn setImage:[UIImage imageNamed:@"seleQuyuyew"] forState:UIControlStateNormal];
    } else {
        [_secSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
    }
    NSLog(@"您点击了 分区 区头 的选择按钮");
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //定义Cell标识 每一个Cell对应一个自己的标识
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section, indexPath.row];
    
    //通过唯一标识创建Cell实例
    MyOrderCarTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    //判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,w而不会再次初始化)
    if (!mycell) {
        mycell = [[MyOrderCarTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    self.mytableView.separatorStyle = UITableViewCellEditingStyleNone;//不显示分割线
    
    mycell.myblock = ^(NSUInteger ind, NSString * _Nonnull str) {
        switch (ind) {
            case 0: {
                self.rowChose =! self.rowChose;
                if (self.rowChose) {
                    [mycell.rowSelBtn setImage:[UIImage imageNamed:@"seleQuyuyew"] forState:UIControlStateNormal];
                } else {
                    [mycell.rowSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
                }
            }
                break;
            case 1: {
                //减号
                KMyLog(@"减一");
                //NSInteger ind;
                NSInteger ind = [mycell.numberLab.text integerValue];
                ind-=1;
                if (ind == 0) {
                    ind = 1;
                }
                mycell.numberLab.text = [NSString stringWithFormat:@"%ld",ind];
                //                model.count = [NSString stringWithFormat:@"%ld",ind];
            }
                break;
            case 2: {
                //加号
                KMyLog(@"加一");
                NSInteger ind = [mycell.numberLab.text integerValue];
                ind+=1;
                mycell.numberLab.text = [NSString stringWithFormat:@"%ld", ind];
                //model.count = [NSString stringWithFormat:@"%ld",ind];
            }
                break;
            case 3: {
                //删除
                //[weakSelf.mydateSource removeObjectAtIndex:indexPath.row];
                //[weakSelf.myTableView reloadData];
                //CGFloat ffftotle = 0;
                //for (CSmakeAccAddmodel *model in self.mydateSource) {
                //    CGFloat motltlt = [model.total_price floatValue];
                //    ffftotle = ffftotle +motltlt;
                //}
            }
                break;
            default:
                break;
        }
    };
    return mycell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

/**
 弹出框的背景图 (候保配件)
 */
-(void)shwoBgviewsecDele{
    self.bgViewsecDele = [[UIView alloc]init];
    self.bgViewsecDele.frame = self.view.bounds;
    self.bgViewsecDele.backgroundColor = RGBA(1, 1, 1, 0.5);
    UIView *whiteBGView = [[UIView alloc]init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.masksToBounds = YES;
    whiteBGView.layer.cornerRadius = 6;
    [self.bgViewsecDele addSubview:whiteBGView];
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.offset(240);
        make.left.offset(22);
        make.right.offset(-22);
    }];
    
    UIImageView *upImage = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH/2-22-30, 34, 60, 60)];
    upImage.layer.masksToBounds = YES;
    upImage.layer.cornerRadius = 20;
    [whiteBGView addSubview:upImage];
    upImage.image = [UIImage imageNamed:@"tipImage"];
    
    UILabel *UpLable = [[UILabel alloc]initWithFrame:CGRectMake(0, upImage.bottom +19, KWIDTH-44, 21)];
    [whiteBGView addSubview:UpLable];
    UpLable.font = FontSize(16);
    UpLable.textColor = [UIColor colorWithHexString:@"#333333"];
    UpLable.textAlignment =  NSTextAlignmentCenter;
    UpLable.text = @"确认删除?";
    
    UIButton *leftButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [leftButton setTitle:@"我在想想" forState:(UIControlStateNormal)];
    [leftButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [leftButton setBackgroundColor:[[UIColor colorWithHexString:@"#70BE68"] colorWithAlphaComponent:1]];
    leftButton.layer.masksToBounds = YES;
    leftButton.layer.cornerRadius = 4;
    leftButton.titleLabel.font = FontSize(14);
    [leftButton addTarget:self action:@selector(tleftBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.offset(28);
        make.right.equalTo(whiteBGView.mas_centerX).offset(-13);
        make.bottom.offset(-19);
    }];
    
    UIButton *rightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [rightButton setTitle:@"删除" forState:(UIControlStateNormal)];
    [rightButton setTitleColor:[UIColor colorWithHexString:@"#70BE68"] forState:(UIControlStateNormal)];
    [rightButton setBackgroundColor:[UIColor whiteColor]];
    rightButton.layer.masksToBounds = YES;
    rightButton.layer.cornerRadius = 4;
    rightButton.layer.borderWidth = 1;
    rightButton.layer.borderColor = [UIColor colorWithHexString:@"#70BE68"].CGColor;
    rightButton.titleLabel.font = FontSize(14);
    [rightButton addTarget:self action:@selector(trightBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.equalTo(whiteBGView.mas_centerX).offset(13);
        make.right.offset(-28);
        make.bottom.offset(-19);
    }];
}

-(void)tleftBtnAction:(UIButton *)but{
    [_bgViewsecDele removeFromSuperview];
    NSLog(@"您点击了 左侧按钮");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)trightBtnAction:(UIButton *)but {
    [_bgViewsecDele removeFromSuperview];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
        _scrollView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(KWIDTH, 100);
        adjustInset(_scrollView);
    }
    return _scrollView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
